package com.gitee.ywj1352.jdk8;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author wenjie.yang
 * @date 2/26/2020 10:42 AM
 */
public class LRU<K,V> {
    private int cacheSize;
    private LinkedHashMap<K, V> cacheMap;
    public LRU(int cacheSize){
        this.cacheSize = cacheSize;
        cacheMap = new LinkedHashMap(16, 0.75F, true){
            @Override
            protected boolean removeEldestEntry(Map.Entry eldest) {
                if(cacheSize + 1 == cacheMap.size()){
                    return true;
                }else{
                    return false;
                }
            }
        };
    }
    public void put(K key, V value){
        cacheMap.put(key, value);
    }
    public V get(K key){
        return cacheMap.get(key);
    }
    public Collection<Map.Entry<K, V>> getAll(){
        return new ArrayList<Map.Entry<K, V>>(cacheMap.entrySet());
    }


}
